/**
 * Created by Administrator on 2016-07-31.domReady.js
 * DataTables
 */

define([//3.加载依赖的库
        'jquery',
        'domReady!',
        'bootstrap',
        'datatables.net-bs',
        // 'datatables.net-buttons',
        'datatables.net-autofill-bs',
        'net-jszip',
        'net-pdfmake',
        'datatables.net-buttons-bs',
        'datatables.net-colreorder',
        'datatables.net-rowreorder',
        'datatables.net-select',
        'datatables.net-scroller',
        'datatables.net-fixedHeader',
        // 'datatables.net-editor',
        'datatables.net-keyTable',
        'datatables.net-editor-bs',
        'datatables.net-responsive',
        'jstree',

    ],
    function ($) {
        console.log("datatables_edit-define:" + new Date().getTime());

        var editor;
        var myDT = {};
        myDT.init = function () {
            editor = new $.fn.dataTable.Editor({
                ajax: "data/DataTables.txt",
                table: "#example",
                fields: [{
                    label: "First name:",
                    name: "first_name"
                }, {
                    label: "Last name:",
                    name: "last_name"
                }, {
                    label: "Position:",
                    name: "position"
                }, {
                    label: "Office:",
                    name: "office",
                    type: "select",
                    options: [
                        {label: "Edinburgh", value: 51},
                        {label: "London", value: 76},
                        {label: "似懂非懂", value: 76},
                        {label: "www", value: 76},
                        {label: "多大的", value: 76},
                        {label: "qqqq", value: 76},
                        {label: "aaa", value: 76},
                    ]
                }, {
                    label: "Extension:",
                    name: "extn"
                }, {
                    label: "Start date:",
                    name: "start_date",
                    type: 'datetime'
                }, {
                    label: "Salary:",
                    name: "salary"
                }
                ]
            });

            var table = $('#example').DataTable({
               language: {
                   //"url": "chinese.lang.json"
                   select: {
                       rows: {
                           _: '%d 行被选择',
                           0: '',
                           1: '1行被选择'
                       }
                   }
               },
                colReorder: true,
                //rowReorder: true,
                lengthChange: false,
                // "bScrollCollapse": false,
                pagingType: 'full_numbers',
                paging: true,
//                "dom": '<"wrapper"flipt>',
                ajax: "data/DataTables.txt",

                scrollX: true,
                scrollY: true,
                // scrollY:        200,
                //deferRender:    true,
                // scroller:       true,
//                fixedColumns: {        /*1.5.4 有bug,错位*/
//                    leftColumns: 2
//                },
//                 responsive: true,

                columns: [
                    {   // Checkbox select column
                        data: null,
                        defaultContent: '',
                        className: 'select-checkbox',
                        orderable: false
                    },
                    {
                        data: null, render: function (data, type, row) {
                        // Combine the first and last names into a single table field
                        return data.first_name + ' ' + data.last_name;
                    }
                    },
                    {data: "position"},
                    {data: "office"},
                    {data: "extn"},
                    {data: "start_date"},
                    {data: "salary", render: $.fn.dataTable.render.number(',', '.', 0, '$')}
                ],

                select: {
                    style: 'os', /* os,single,multi, multi+shift,api*/
                    selector: 'td.select-checkbox',
//                    items: 'cell', //'row,column,cell',
                    //blurable: true
                },
                // keys: {
                //     columns: ':not(:first-child)',
                //     editor:  editor
                // },
                // autoFill: {
                //     editor: editor
                // },
//                select: true
            });

            // Display the buttons
            new $.fn.dataTable.Buttons(table, [
                {extend: "create", text: '新建', editor: editor},
                {extend: "edit", text: '编辑', editor: editor},
                {extend: "remove", text: '删除', editor: editor},
                {extend: "colvis", text: '列', editor: editor},
//                {extend: "columnsToggle", editor: editor},
//                {extend: "columnsVisibility", editor: editor},
//                {extend: "columnToggle", editor: editor},
//                {extend: "colvisGroup", editor: editor},
//                {extend: "colvisRestore", editor: editor},
//                 {extend: "copyHtml5", editor: editor},
//                 {extend: "excelHtml5", editor: editor},
//                 {extend: "pageLength", editor: editor},
                {
                    extend: 'collection',
                    text: '输出',
                    buttons: [
                        'copyHtml5',
                        'excel',
                        'csv',
                        'pdf',
                        'print'
                    ],

                },
                {
                    extend: 'collection',
                    text: '选择',
                    buttons: [
                        'selectAll',
                        'selectCells',
                        'selectColumns',
                        'selected',
                        'selectedSingle',
                        'selectNone',
                        'selectRows'
                    ],

                }
            ]);

            table.buttons().container().appendTo($('.col-sm-6:eq(0)', table.table().container()));

            //启用表内编辑
            $(table.table().container()).on('click', 'tbody td:not(:first-child)', function (e) {
                var index = $(this).index();

                if (index === 1) {
                    editor.bubble(this, ['first_name', 'last_name'], {
                        title: '编辑姓名:'
                    });
                }
                else {
                    editor.inline(this);
                    /*行内编辑*/
                }
                //editor.bubble(this);              /*气泡编辑*/
//
            });

            // new $.fn.dataTable.FixedHeader(table, {
            //     header: true,
            //     // footer: true,
            //     // headerOffset: $('nav').height(),
            //     headerOffset: $('nav div.container').height()
            // });

            //Tree
            $("#button1").bind("click", function demo_create() {
                var ref = $('#jstree_demo').jstree(true),
                    sel = ref.get_selected();
                if (!sel.length) {
                    return false;
                }
                sel = sel[0];
                sel = ref.create_node(sel, {"type": "file"});
                if (sel) {
                    ref.edit(sel);
                }
            });

            $("#button2").bind("click", function demo_rename() {
                var ref = $('#jstree_demo').jstree(true),
                    sel = ref.get_selected();
                if (!sel.length) {
                    return false;
                }
                sel = sel[0];
                ref.edit(sel);
            })
            ;
            $("#button3").bind("click", function demo_delete() {
                var ref = $('#jstree_demo').jstree(true),
                    sel = ref.get_selected();
                if (!sel.length) {
                    return false;
                }
                ref.delete_node(sel);
            });
            $(function () {
                var to = false;
                $('#demo_q').keyup(function () {
                    if (to) {
                        clearTimeout(to);
                    }
                    to = setTimeout(function () {
                        var v = $('#demo_q').val();
                        $('#jstree_demo').jstree(true).search(v);
                    }, 250);
                });

                $('#jstree_demo')
                    .jstree({
                        "core": {
                            "animation": 0,
                            "check_callback": true,
                            'force_text': true,
                            "themes": {"stripes": true},
                            'data': {
                                'url': function (node) {
                                    return node.id === '#' ? 'data/ajax_demo_roots.json' : 'data/ajax_demo_children.json';
                                },
                                'data': function (node) {
                                    return {'id': node.id};
                                }
                            }
                        },
                        "types": {
                            "#": {"max_children": 1, "max_depth": 4, "valid_children": ["root"]},
                            "root": {"valid_children": ["default"]},
                            "default": {"valid_children": ["default", "file"]},
                            "file": {"icon": "glyphicon glyphicon-file", "valid_children": []}
                        },
                        "plugins": ["contextmenu", "dnd", "search", "state", "types", "wholerow"]
                    });
            });

        };

        require(myDT.init());

        return myDT;
    }
);
